package com.zle.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zle.pojo.Book;
import com.zle.pojo.Person;
import com.zle.pojo.Persons;

@Controller
@RequestMapping("/test")
public class TestController {

	private static Logger logger = LoggerFactory.getLogger(TestController.class);

	/**
	 * 基本类型必须传值,会报错,封装类型不传值默认为null..
	 * 
	 * @RequestParam可以不写
	 * 
	 * @param age
	 * @return
	 */
	@RequestMapping(value = "/type")
	@ResponseBody
	public Book baseType(@RequestParam(value = "age", defaultValue = "100") int age) {
		Book b = new Book();
		List<Person> l = new ArrayList<Person>();
		Person p = new Person();
		p.setName("1");
		p.setSex("男");
		l.add(p);

		p = new Person();
		p.setName("2");
		p.setSex("女");
		l.add(p);
//		b.setPersons(l);
//		b.setName("java");
//		b.setPrice(50);
		return b;
	}

	/**
	 * 数组传递
	 * http://localhost/springmvc/test/type2?name=%22a%22&age=1&name=%22a111%22
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping("/type2")
	@ResponseBody
	public String baseType2(String name[]) {
		StringBuilder sb = new StringBuilder();
		for (String s : name) {
			sb.append(s);
		}
		return sb.toString();
	}

	/**
	 * 对象绑定,其中有嵌套对象,List的传递方式
	 * http://localhost/springmvc/test/type3?name=%22a%22&contact.qq=3346&phone=
	 * 18612261449&hobby=1&hobby=2
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping("/type3")
	@ResponseBody
	public String baseType3(Person person) {
		
		return ReflectionToStringBuilder.toString(person);
	}

	/**
	 * Listc赋值,必须将List对象放到一个对象中
	 * 
	 * http://localhost/springmvc/test/type4?person[0].name=%22fdsf%22&person[1]
	 * .age=20 如果序列直接写多了前面会出现空对象person[10],会出现9个空对象
	 * 
	 * @return
	 */
	@RequestMapping("/type4")
	@ResponseBody
	public String type4(Persons per) {
		StringBuilder sb = new StringBuilder();
		for (Person p : per.getPerson()) {
			sb.append(ReflectionToStringBuilder.toString(p));
		}
		return sb.toString();
	}

	@RequestMapping("/type5")
	@ResponseBody
	public String type5(@RequestBody Person per) {

		return ReflectionToStringBuilder.toString(per);
	}

	@RequestMapping("/show")
	public String show() {

		return "/hello/test";
	}

	@RequestMapping(value = "/req/{pid}", method = RequestMethod.GET)
	@ResponseBody
	public String getDemo(@PathVariable String pid) {

		return "get " + pid;
	}

	@RequestMapping(value = "/req/{pid}", method = RequestMethod.POST)
	@ResponseBody
	public String postDemo(@PathVariable String pid) {

		return "post" + pid;
	}

	@RequestMapping(value = "/req/{pid}", method = RequestMethod.PUT)
	@ResponseBody
	public String putDemo(@PathVariable String pid) {
		ModelAndView modelAndView = new ModelAndView();
		return "put" + pid;
	}

	@RequestMapping(value = "/req/{pid}/{name}", method = RequestMethod.DELETE)
	@ResponseBody
	public String deleteDemo(@PathVariable String pid, @PathVariable String name, Integer age) {

		return "delete  pid:" + pid + "   name" + name + "  age:" + age;
	}

}
